package com.intelligent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.intelligent.entity.UserNotice;
import com.intelligent.service.UserNoticeService;
import com.intelligent.mapper.UserNoticeMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lenovo
 * @description 针对表【user_notice(用户通知关联表)】的数据库操作Service实现
 * @createDate 2025-09-02 20:25:19
 */
@Service
public class UserNoticeServiceImpl extends ServiceImpl<UserNoticeMapper, UserNotice>
        implements UserNoticeService {

    @Override
    @Transactional
    public Boolean createUserNotices(Integer noticeId, List<Long> receiverIds) {
        if (noticeId == null || receiverIds == null || receiverIds.isEmpty()) {
            return false;
        }

        List<UserNotice> userNotices = receiverIds.stream().map(userId -> {
            UserNotice userNotice = new UserNotice();
            userNotice.setNoticeId(noticeId);
            userNotice.setUserId(userId);
            userNotice.setReadStatus(0); // 未读
            userNotice.setCreateTime(new Date());
            return userNotice;
        }).collect(Collectors.toList());

        return saveBatch(userNotices);
    }

    @Override
    public UserNotice getUserNoticeStatus(Integer noticeId, Long userId) {
        if (noticeId == null || userId == null) {
            return null;
        }

        LambdaQueryWrapper<UserNotice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserNotice::getNoticeId, noticeId)
                .eq(UserNotice::getUserId, userId);

        return getOne(queryWrapper);
    }

    @Override
    @Transactional
    public Boolean markAsRead(Integer noticeId, Long userId) {
        if (noticeId == null || userId == null) {
            return false;
        }

        LambdaUpdateWrapper<UserNotice> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(UserNotice::getNoticeId, noticeId)
                .eq(UserNotice::getUserId, userId)
                .set(UserNotice::getReadStatus, 1)
                .set(UserNotice::getReadTime, new Date());

        return update(updateWrapper);
    }

    @Override
    public Integer getUnreadCount(Long userId) {
        if (userId == null) {
            return 0;
        }

        LambdaQueryWrapper<UserNotice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserNotice::getUserId, userId)
                .eq(UserNotice::getReadStatus, 0);

        return Math.toIntExact(count(queryWrapper));
    }

    @Override
    @Transactional
    public Boolean batchMarkAsRead(List<Integer> noticeIds, Long userId) {
        if (noticeIds == null || noticeIds.isEmpty() || userId == null) {
            return false;
        }

        LambdaUpdateWrapper<UserNotice> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(UserNotice::getNoticeId, noticeIds)
                .eq(UserNotice::getUserId, userId)
                .set(UserNotice::getReadStatus, 1)
                .set(UserNotice::getReadTime, new Date());

        return update(updateWrapper);
    }

}
